package com.heima.behavior.service.impl;

import com.heima.apis.article.IArticleClient;
import com.heima.behavior.service.BehaviorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppUserThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author Psycho
 * @create 2022-05-03-12:18
 */
@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private IArticleClient articleClient;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 点赞或者取消点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeOrNotLike(LikesBehaviorDto dto) {

        //取出当前登入的用户
        ApUser user = AppUserThreadLocalUtil.getUser();

        //校验数据
        if(dto.getArticleId()!=null){
            //查找出ApArticle文章
            ApArticle article = articleClient.getOneArticle(dto.getArticleId());
            //文章不为null
            if(article == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //是取消点赞还是点赞
            if(dto.getOperation()==0){
                article.setLikes(Objects.isNull(article.getLikes())?1:article.getLikes()+1);
            }else {
                //如果是取消点赞，文章的likes字段递减1
                article.setLikes(article.getLikes()-1);
            }
            //保存article到数据库
            articleClient.insertOne(article);

            //Redis存入Hash结构的数据
            //Key值
            String firstKey = article.getId()+"_";
            String secondKey = user.getId()+"_Like";
            if(dto.getOperation()==0){
                redisTemplate.opsForHash().put(firstKey,secondKey,0);
            }else {
                redisTemplate.opsForHash().put(firstKey,secondKey,1);
            }
            return ResponseResult.okResult(200);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 文章的查看次数
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readArticleCount(ReadBehaviorDto dto) {
        //校验数据
        if(dto.getArticleId()!=null){
            //查找出ApArticle文章
            ApArticle article = articleClient.getOneArticle(dto.getArticleId());
            //文章不为null
            if(article == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //文章的浏览次数进行自增
            if(dto.getCount() != 1){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"浏览异常");
            }
            article.setViews(Objects.isNull(article.getViews())?1:article.getViews()+1);
            //保存article到数据库
            articleClient.insertOne(article);
            String ArticleViewsKey = article.getId()+"_Views";
            redisTemplate.opsForValue().set(ArticleViewsKey,article.getViews());
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}
